package br.com.foxline.atlasmobile;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import br.com.foxline.atlasmobile.novocadastro.CriarImovelActivity;
import br.com.foxline.atlasmobile.util.SharedPreference;
import br.com.foxline.atlasmobile.util.UtilConnection;

public class MenuActivity extends Activity {

	private final OnClickListener listener = new OnClickListener() {
		private Intent intent;

		@Override
		public void onClick(View view) {

			switch (view.getId()) {
			case R.id.create:
				intent = new Intent(getApplicationContext(),
						CreateMainActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
				break;
			case R.id.show:
				intent = new Intent(getApplicationContext(),
						ShowCadastroActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
				break;
			case R.id.sync:
				intent = new Intent(getApplicationContext(),
						ExportActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
				break;
			case R.id.config:

				intent = new Intent(getApplicationContext(),
						ConfigPortActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
				break;
			case R.id.teste:
				intent = new Intent(getApplicationContext(),
						CriarImovelActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.slide_in_right,
						R.anim.slide_out_left);
				break;
			default:
				break;
			}

		}
	};
	private AlertDialog dialog;
	private SharedPreferences prefs;
	private Menu menu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);

		((Button) findViewById(R.id.create)).setOnClickListener(listener);
		((Button) findViewById(R.id.show)).setOnClickListener(listener);
		((Button) findViewById(R.id.sync)).setOnClickListener(listener);
		((Button) findViewById(R.id.config)).setOnClickListener(listener);
		((Button) findViewById(R.id.teste)).setOnClickListener(listener);

		prefs = this.getSharedPreferences(SharedPreference.PREFS_NAME, 0);

		if (getIntent().getBooleanExtra("grupo", false)) {
			criarDialog();
			dialog.show();
		}

	}

	public void criarDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Grupo de Cadastro");

		final String[] items = new String[] { "GRUPO 1", "GRUPO 2", "GRUPO 3",
				"GRUPO 4", "GRUPO 5", "GRUPO 6", "GRUPO 7", "GRUPO 8" };

		builder.setItems(items, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Editor editor = prefs.edit();
				editor.putString(SharedPreference.GROUPS, items[which] + "");
				editor.commit();
				menu.findItem(R.id.group_name).setTitle(
						prefs.getString(SharedPreference.GROUPS, "GRUPO"));
			}
		});

		dialog = builder.create();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_menu, menu);
		this.menu = menu;
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.group_name:
			dialog.show();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
